// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LyroRS

//@version=6
indicator("Pulse RSI | Lyro RS", overlay = false)


//─────────────────────────────────────────────────────────────────────────────────────────────────────────────
//─██████─────────████████──████████─████████████████───██████████████───────████████████████───██████████████─
//─██░░██─────────██░░░░██──██░░░░██─██░░░░░░░░░░░░██───██░░░░░░░░░░██───────██░░░░░░░░░░░░██───██░░░░░░░░░░██─
//─██░░██─────────████░░██──██░░████─██░░████████░░██───██░░██████░░██───────██░░████████░░██───██░░██████████─
//─██░░██───────────██░░░░██░░░░██───██░░██────██░░██───██░░██──██░░██───────██░░██────██░░██───██░░██─────────
//─██░░██───────────████░░░░░░████───██░░████████░░██───██░░██──██░░██───────██░░████████░░██───██░░██████████─
//─██░░██─────────────████░░████─────██░░░░░░░░░░░░██───██░░██──██░░██───────██░░░░░░░░░░░░██───██░░░░░░░░░░██─
//─██░░██───────────────██░░██───────██░░██████░░████───██░░██──██░░██───────██░░██████░░████───██████████░░██─
//─██░░██───────────────██░░██───────██░░██──██░░██─────██░░██──██░░██───────██░░██──██░░██─────────────██░░██─
//─██░░██████████───────██░░██───────██░░██──██░░██████─██░░██████░░██───────██░░██──██░░██████─██████████░░██─
//─██░░░░░░░░░░██───────██░░██───────██░░██──██░░░░░░██─██░░░░░░░░░░██───────██░░██──██░░░░░░██─██░░░░░░░░░░██─
//─██████████████───────██████───────██████──██████████─██████████████───────██████──██████████─██████████████─
//─────────────────────────────────────────────────────────────────────────────────────────────────────────────
//                                                 LyroRS v1.0


var indi_g = "Indicator Settings"
var colors_g = "Color Settings"
var plot_g = "Plot Settings"
var signal_g = "Signal Settings"

// Inputs
len = input.int(55, title = "Length", tooltip = "Adjust the Length of the Indicator", group = indi_g)
Threshold_L_Fl = input.int(10, "Threshold Long", step=1, group = indi_g, tooltip = "Adjust Threshold for Long signals to Fire, NOTE: Enable Type 4")
Threshold_S_Fl = input.int(15, "Threshold Short", step=1, group = indi_g, tooltip = "Adjust Threshold for Short signals to Fire, NOTE: Enable Type 4")


// Plots Enable section
sigType1Enable = input.bool(false, title = "Trend - Type 1", tooltip = "Enable Type 1 Conditions (res > res[1]), Trend", group = signal_g)
sigType2Enable = input.bool(true, title = "Trend - Type 2", tooltip = "Enable Type 2 Conditions (res > 50), Trend", group = signal_g)
sigType3Enable = input.bool(true, title = "Oversold / Overbought", tooltip = "Enable Type 3 Conditions (res > 30 / 70), Oversold and Overbought", group = signal_g)
sigType4Enable = input.bool(false, title = "Custom Threshold L/S", tooltip = "Enable Type 4 Conditions (res > Threshold Long / Short), Custom Signals", group = signal_g)

// Color Inputs
ColMode = input.string("Mystic", "Custom Color Palette", inline="drop", options=["Classic", "Mystic", "Accented", "Royal"], display=display.none, group=colors_g,  tooltip="Choose a predefined color scheme for indicator visualization.")

cpyn = input.bool(false, "Use Custom Palette", tooltip= "Enable manual selection of custom colors for trend signals.", group=colors_g, display=display.none)
cp_UpC = input.color(#00ff00, "Custom Up", inline= "Custom Palette", tooltip= "Set a custom color for bullish signals.", group=colors_g, display=display.none)
cp_DnC = input.color(#ff0000, "Custom Down", inline= "Custom Palette", tooltip= "Set a custom color for bearish signals.", group=colors_g, display=display.none)



// Colors
color UpC = na
color DnC = na

switch ColMode
    "Classic" =>
        UpC := #00E676
        DnC := #880E4F
    "Mystic" =>
        UpC := #30FDCF
        DnC := #E117B7
    "Accented" =>
        UpC := #9618F7
        DnC := #FF0078
    "Royal" =>
        UpC := #FFC107
        DnC := #673AB7


if cpyn
    UpC := cp_UpC
    DnC := cp_DnC

// Method
vol = (close * high * low) * math.abs(volume)
help = math.sqrt(vol)
helpp = ta.sma(help, len)
help2 = ta.linreg(helpp, len, 0)
u = math.max(help2 - help2[1], 0)
d = math.max(help2[1] - help2, 0)
rs = ta.ema(u, len) / ta.ema(d, len)
res = 100 - 100 / (1 + rs)

// Conditions
// Trend
var sigType1 = 0
var colType1 = color.white
if res > res[1]
    sigType1 := 1
    colType1 := UpC
if res < res[1]
    sigType1 := -1
    colType1 := DnC

// Trend
var sigType2 = 0
var colType2 = color.white
if res > 50
    sigType2 := 1
    colType2 := UpC
if res < 50
    sigType2 := -1
    colType2 := DnC

// Oversold / Overbought
var sigType3 = 0
var colType3 = color.white
if res > 30
    sigType3 := 1
    colType3 := UpC
if res < 70
    sigType3 := -1
    colType3 := DnC

// Threshold Long / Short Custom Signals
var sigType4 = 0
var colType4 = color.white
if res > Threshold_L_Fl
    sigType4 := 1
    colType4 := UpC
if res < Threshold_S_Fl
    sigType4 := -1
    colType4 := DnC


// Color
pc_dd = sigType1Enable and res > res[1] ? UpC : sigType1Enable and res < res[2] ? DnC : sigType2Enable and res > 50 ? UpC : sigType2Enable and res < 50 ? DnC : na

// Plots
plotshape(sigType1Enable ? ta.crossover(res, res[1]) : false, title = "Type 1 Signals - Trend", style = shape.labelup,text = "𝓛𝓸𝓷𝓰",textcolor = #000000, force_overlay = true, size = size.tiny, location = location.belowbar, color = colType1)
plotshape(sigType1Enable ? ta.crossunder(res, res[1]) : false, title = "Type 1 Signals - Trend", style = shape.labeldown,text = "𝓢𝓱𝓸𝓻𝓽",textcolor = #000000, force_overlay = true, size = size.tiny, location = location.abovebar, color = colType1)

plotshape(sigType2Enable ? ta.crossover(res, 50) : false, title = "Type 2 Signals - Trend", style = shape.labelup,text = "𝓛𝓸𝓷𝓰",textcolor = #000000, force_overlay = true, size = size.tiny, location = location.belowbar, color = colType2)
plotshape(sigType2Enable ? ta.crossunder(res, 50) : false, title = "Type 2 Signals - Trend", style = shape.labeldown,text = "𝓢𝓱𝓸𝓻𝓽",textcolor = #000000, force_overlay = true, size = size.tiny, location = location.abovebar, color = colType2)

plotshape(sigType3Enable ? ta.crossover(res, 30) : false, title = "Type 3 Signals - Oversold / Overbought", style = shape.triangleup, force_overlay = true, size = size.tiny, location = location.belowbar, color = UpC)
plotshape(sigType3Enable ? ta.crossunder(res, 70) : false, title = "Type 3 Signals - Oversold / Overbought", style = shape.triangledown, force_overlay = true, size = size.tiny, location = location.abovebar, color = DnC)

plotshape(sigType4Enable ? ta.crossover(res, Threshold_L_Fl) : false, title = "Type 4 Signals - Custom Threshold L/S", style = shape.labelup,text = "⬆", textcolor = color.white, force_overlay = true, size = size.tiny, location = location.belowbar, color = UpC)
plotshape(sigType4Enable ? ta.crossunder(res, Threshold_S_Fl) : false, title = "Type 4 Signals - Custom Threshold L/S", style = shape.labeldown,text = "⬇", textcolor = color.white, force_overlay = true, size = size.tiny, location = location.abovebar, color = DnC)


pg = plot(res, color = sigType1Enable and res > res[1] ? UpC : sigType1Enable and res < res[2] ? DnC : sigType2Enable and res > 50 ? UpC : sigType2Enable and res < 50 ? DnC : na, linewidth = 2, title = "Main Plot Line")
hline(50, title = "MidLine")

pc_flType1 = sigType1Enable and res > res[1] ? UpC : sigType1Enable and res < res[2] ? DnC : sigType2Enable and res > 50 ? UpC : sigType2Enable and res < 50 ? DnC : na

plotcandle(open, high, low, close, "Candle Color", pc_flType1, pc_flType1, true, bordercolor = pc_flType1, force_overlay = true, display=display.pane)


// -- RRR TH RRR Plot
plot(res, color = color.new(pc_dd, 75), title= "Glow Effect", linewidth = 10, display= display.pane)

plottlfl = plot(Threshold_L_Fl, color= color.new(UpC,30), editable = false)
plottsfl = plot(Threshold_S_Fl, color= color.new(DnC,30), editable = false)


// Alerts
alertcondition(res > res[1], "Pulse RSI Long / Type 1-Trend", "Pulse RSI Trend is Long {{exchange}}:{{ticker}}")
alertcondition(res < res[1], "Pulse RSI Short / Type 1-Trend", "Pulse RSI Trend is Short {{exchange}}:{{ticker}}")


alertcondition(res > 50, "Pulse RSI Long / Type 2-Trend", "Pulse RSI Trend is Long {{exchange}}:{{ticker}}")
alertcondition(res < 50, "Pulse RSI Short / Type 2-Trend", "Pulse RSI Trend is Short {{exchange}}:{{ticker}}")


alertcondition(res > 30, "Pulse RSI Long / Type 3-Oversold/Overbought", "Pulse RSI Oversold is Long {{exchange}}:{{ticker}}")
alertcondition(res < 70, "Pulse RSI Short / Type 3-Oversold/Overbought", "Pulse RSI Overbought is Short {{exchange}}:{{ticker}}")


alertcondition(res > Threshold_L_Fl, "Pulse RSI Long / Type 4-Custom Threshold L/S", "Pulse RSI Threshold HIT Long {{exchange}}:{{ticker}}")
alertcondition(res < Threshold_S_Fl, "Pulse RSI Short / Type 4-Custom Threshold L/S", "Pulse RSI Threshold HIT Short {{exchange}}:{{ticker}}")
